package com.kunchi.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.kunchi.platform.entity.admin.Company;
import com.kunchi.platform.model.admin.LoginInfo;
import com.kunchi.platform.service.CompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by denniszhou on 2017/7/27.
 */
@Controller
@RequestMapping(value = "/company")
public class CompanyController {
    @Autowired
    CompanyService companyService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public LoginInfo isAllowed(@RequestBody JSONObject jsonObject) {
        LoginInfo loginInfo = new LoginInfo();
        String name = jsonObject.getString("name");
        String password = jsonObject.getString("password");
        Long id = companyService.isAllowed(name, password);
        if (id != null) {
            loginInfo.setCode(200);
            loginInfo.setId(id);
            loginInfo.setName(name);
            loginInfo.setMessage("登录成功!");
        } else {
            loginInfo.setCode(401);
            loginInfo.setMessage("用户名或密码错误!");
        }
        return loginInfo;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Company save(@RequestBody JSONObject jsonObject) {
        Company company = JSONObject.parseObject(jsonObject.toJSONString(), Company.class);
        return companyService.save(company);
    }
}
